import {Component} from '@angular/core';
import {App, NavController} from 'ionic-angular';

import {ProductInfoPage} from './ProductInfo/ProductInfo';

//发送http请求
import {Http,Headers} from '@angular/http';

import {servicess} from '../../provider/service'
import {interfaceUrls} from '../../provider/serverUrls';	//没有真实数据暂时不需要引入接口


import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';
import {HomeServie} from "./home.service";
import {services} from "../../providers/service";
import {UserService} from "../../providers/user-service";
import {AppPopup} from "../../util/popup";
import {UserLogin} from "../../modules/app-user/user-login/user-login";
//引入登录页面的ID


@Component({
  selector: 'page-home',
    templateUrl: 'home.html',
    providers: [HomeServie]
})

export class HomePage {
	items = [];
    param = {};

	pInfo = {};

    cityData: any[]; //城市数据
    cityName: string = ''; //初始化城市名
    code: string = '320102'; //城市编码

    pageNo = 1;     //当前页
    pageSize = 10;  //每页条数
    count = 0;      //计算总页数与    cout(数量相区别这里是总页数)

	//下拉刷新
	items2 = [];

    produNull = false;    //如果当前库存为0，则显示此提示

    //http请求
    private headers = new Headers({'Content-Type': 'application/json;chartset=utf-8'});

    constructor(public navCtrl: NavController,
                public http: Http,
                private app: App,
                public service: servicess,
                private userServ: UserService,
                private popup: AppPopup,
                public services: services,
                public homeservice: HomeServie) {
        console.info(this.services.infoData.loginName);
        //地区数据
        this.homeservice.getCitiesData()
            .then(data => {
                this.cityData = data;
                this.services.cityData = data;
                console.log("城市数据", this.cityData);
                // this.compareCode(this.code);
            });
    }

    goProductInfo() {
        this.navCtrl.push(ProductInfoPage);
    }

    handleError(err) {
        console.info(err)
    }

    doRefresh(refresher) {
        //下拉刷新发货渠道
        console.log('Begin async operation', refresher);
        this.pageNo = 1;
        this.http
            .post(`${interfaceUrls.repertoryList}?pageNo=${this.pageNo}&pageSize=${this.pageSize}`, JSON.stringify(this.param), {headers: this.headers})
            .timeout(2000)
            .toPromise()
            .then(response => {
                console.info(response.json().infoData.items)
                this.items = response.json().infoData.items;
                refresher.complete();	//刷新完成
            })
            .catch(err => {
                console.log('err');
            });
        //下拉刷新收货渠道查询

    }


    doInfinite(infiniteScroll) {
        //上拉加载..
        if (this.count > this.pageNo) {
            this.pageNo += 1;
            this.http
                .post(`${interfaceUrls.repertoryList}?pageNo=${this.pageNo}&pageSize=${this.pageSize}`, JSON.stringify(this.param), {headers: this.headers})
                .timeout(2000)
                .toPromise()
                .then(response => {
                    console.info(response.json().infoData.items);
                    // this.items = response.json().infoData.items;
                    this.items = this.items.concat(response.json().infoData.items);
                    infiniteScroll.complete();	//加载完成

                })
                .catch(err => {
                    console.log('err');
                });
        } else {
            infiniteScroll.complete();	//加载完成
        }

    }

    ionViewWillEnter() {
        //获取个人信息
        this.userServ.getPersonInfo()
            .then(res => {
                console.info("用户详情获取成功：", res);
                this.pInfo = res.infoData;
                this.services.personalDetail = res.infoData;
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        })
                } else {
                    this.popup.alert(err.errorMassage);
                }

            });

        this.http
            .post(`${interfaceUrls.repertoryList}?pageNo=${this.pageNo}&pageSize=${this.pageSize}`, JSON.stringify(this.param), {headers: this.headers})
            .timeout(2000)
            .toPromise()
            .then(response => {
                console.info(response.json().infoData.items);
                let data = response.json().infoData.items;
                if (data == "") {
                    console.info("当前无库存...");
                    this.produNull = true;
                } else {
                    this.produNull = false;

                }
                this.items = response.json().infoData.items;
                this.count = Math.ceil(response.json().infoData.count / this.pageSize);
            })
            .catch(err => {
                console.log('err');
            });
    }
}

